package controller;

import helper.UserSession;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;

import databeans.Customer;
import databeans.Employee;
import databeans.Fund;
import databeans.FundHistory;

import model.FundDAO;
import model.FundHistoryDAO;
import model.FundHistoryDAOSQL;
import model.Model;

public class CustomerResearchFundAction extends Action {

	
	private FundHistoryDAO fundHistoryDAO;
	private FundDAO fundDAO;
	private FundHistoryDAOSQL fundHistorySQLDAO;

	public CustomerResearchFundAction(Model model) {
		
		fundHistoryDAO = model.getFundHistoryDAO();
		fundDAO = model.getFundDAO();
		fundHistorySQLDAO = model.getFundHistoryDAOSQL();
	}

	@Override
	public String getName() {
		return "customerResearchFund.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		
		// define target page
		String loginPage = "customer/customer-login.jsp";
		String originPage = "customer/research-customer-funds.jsp";
		String successPage = "customer/research-fund-results.jsp";

		// set the employee
		Employee employee = null;
		Customer customer = null;
		// errors and messages will be stored here
		// resetting those containers
		List<String> errors = new ArrayList<String>();
		List<String> messages = new ArrayList<String>();

		// define the error and message
		request.setAttribute("errors", errors);
		request.setAttribute("messages", messages);
		request.setAttribute("page", "research-funds");
		
        //check if customer has actually logged in
        employee = UserSession.getLoggedInEmployee(request);
        if (employee != null) {
            UserSession.removeEmployeeFromSession(request);
            errors.add("This section is closed for employee");
            return loginPage ;
        }
        
        //check if customer has actually logged in
        customer = UserSession.getLoggedInCustomer(request);
        if (customer == null) {
            errors.add("You need to log in");
            return loginPage;
        }
		
		//Load all the fund
        try {
            Fund [] fundArray = fundDAO.getFunds();
            request.setAttribute("funds", fundArray);
            
            if (fundArray == null){
                messages.add("Currently there is no Funds you can select");
            }
        } catch (DAOException e2) {
            errors.add("funds could not be retrieved");
            //errors.add("msg: "+ e2.getMessage());
            return originPage;
        }
        
		String type = request.getParameter("txtType");
		int fundId = 0;
		
		if(type==null)
		{
			messages.add("Choose a fund to get Research Data for");
			return originPage;
		}
		fundId = Integer.parseInt(request.getParameter("txtFundID"));	
		if(type.equals("WEEK"))
		{
			request.setAttribute("research_type",1);
			try {
				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.DAY_OF_MONTH, -7);
				
			    FundHistory[] fundHistoryList = fundHistorySQLDAO.lookupFromtoNow(fundId, cal);
			    
			    System.out.println("got fund history list = " + fundHistoryList.length);
				Fund f = fundDAO.lookup(fundId);
				
				String fundName = f.getName();
				
				System.out.println("fundName = " + fundName);
				
				if (fundHistoryList.length <=0) {
					messages.add("There is no position for customer " + customer.getFirstName() + " currently available to be seen");
					return originPage;
				}
				
				System.out.println("here-2");

				// Attach (this copy of) the employee bean to the session
				HttpSession session = request.getSession(true);
				session.setAttribute("customer", customer);
				request.setAttribute("fundHistoryList", fundHistoryList);
				request.setAttribute("fundName", fundName);

				// set the welcome message
				//messages.add("Hi " + customer.getFirstName() + "!, belows are the function History");
				return successPage;
			    
			} catch (Exception e) {
				System.out.println(e.getMessage());
				System.out.println(e.getCause());
				errors.add("Unable to retrieve fund History " + e.getMessage());
				return originPage;
			}
		}
		else if(type.equals("ONE"))
		{
			request.setAttribute("research_type",3);
			try {
	            int month = Calendar.getInstance().get(Calendar.MONTH)+1;
	            if(month>1)
	            {
	            	month = month -1; 
	            }
	            else
	            {
	            	month = 12;
	            }
	            Calendar cal = Calendar.getInstance();
	            cal.set(Calendar.MONTH, month-1);
			    FundHistory[] fundHistoryList = fundHistorySQLDAO.lookupFromtoNow(fundId, cal);
			    
			    System.out.println("got fund history list = " + fundHistoryList.length);
				Fund f = fundDAO.lookup(fundId);
				
				String fundName = f.getName();
				
				System.out.println("fundName = " + fundName);
				
				if (fundHistoryList.length <=0) {
					messages.add("There is no position for customer " + customer.getFirstName() + " currently available to be seen");
					return originPage;
				}
				
				System.out.println("here-2");

				// Attach (this copy of) the employee bean to the session
				HttpSession session = request.getSession(true);
				session.setAttribute("customer", customer);
				request.setAttribute("fundHistoryList", fundHistoryList);
				request.setAttribute("fundName", fundName);

				// set the welcome message
				//messages.add("Hi " + customer.getFirstName() + "!, belows are the function History");
				return successPage;
			    
			} catch (Exception e) {
				System.out.println(e.getMessage());
				System.out.println(e.getCause());
				errors.add("Unable to retrieve fund History " + e.getMessage());
				return originPage;
			}
		}
		else if(type.equals("THREE"))
		{
			 request.setAttribute("research_type",7);
			try {
				Calendar cal = Calendar.getInstance();
				int month = cal.get(Calendar.MONTH)+1;
				int year = cal.get(Calendar.YEAR);
	            if(month>3)
	            {
	            	month = month -3; 
	            }
	            else
	            {
	            	month = month+9;
	            	cal.set(Calendar.YEAR,year-1 );
	            }
	           
	            cal.set(Calendar.MONTH, month-1);
	            
			    FundHistory[] fundHistoryList = fundHistorySQLDAO.lookupFromtoNow(fundId, cal);
			    
			    System.out.println("got fund history list = " + fundHistoryList.length);
				Fund f = fundDAO.lookup(fundId);
				
				String fundName = f.getName();
				
				System.out.println("fundName = " + fundName);
				
				if (fundHistoryList.length <=0) {
					messages.add("There is no position for customer " + customer.getFirstName() + " currently available to be seen");
					return originPage;
				}
				
				System.out.println("here-2");

				// Attach (this copy of) the employee bean to the session
				HttpSession session = request.getSession(true);
				session.setAttribute("customer", customer);
				request.setAttribute("fundHistoryList", fundHistoryList);
				request.setAttribute("fundName", fundName);

				// set the welcome message
				//messages.add("Hi " + customer.getFirstName() + "!, belows are the function History");
				return successPage;
			    
			} catch (Exception e) {
				System.out.println(e.getMessage());
				System.out.println(e.getCause());
				errors.add("Unable to retrieve fund History " + e.getMessage());
				return originPage;
			}
		}
		else if(type.equals("HALF"))
		{
			request.setAttribute("research_type",10);
			try {
				Calendar cal = Calendar.getInstance();
				int month = cal.get(Calendar.MONTH)+1;
				int year = cal.get(Calendar.YEAR);
	            if(month>6)
	            {
	            	month = month -6; 
	            }
	            else
	            {
	            	month = month+6;
	            	cal.set(Calendar.YEAR,year-1 );
	            }
	           
	            cal.set(Calendar.MONTH, month-1);
				
			    FundHistory[] fundHistoryList = fundHistorySQLDAO.lookupFromtoNow(fundId, cal);
			    
			    System.out.println("got fund history list = " + fundHistoryList.length);
				Fund f = fundDAO.lookup(fundId);
				
				String fundName = f.getName();
				
				System.out.println("fundName = " + fundName);
				
				if (fundHistoryList.length <=0) {
					messages.add("There is no position for customer " + customer.getFirstName() + " currently available to be seen");
					return originPage;
				}
				
				System.out.println("here-2");

				// Attach (this copy of) the employee bean to the session
				HttpSession session = request.getSession(true);
				session.setAttribute("customer", customer);
				request.setAttribute("fundHistoryList", fundHistoryList);
				request.setAttribute("fundName", fundName);

				// set the welcome message
				//messages.add("Hi " + customer.getFirstName() + "!, belows are the function History");
				return successPage;
			    
			} catch (Exception e) {
				System.out.println(e.getMessage());
				System.out.println(e.getCause());
				errors.add("Unable to retrieve fund History " + e.getMessage());
				return originPage;
			}
		}
		return originPage;
		
	}

}
